#ifndef _Shell_h_
#define _Shell_h_

#include <qlineedit.h>
#include <qtextedit.h>
#include <qprocess.h>
#include <qhbox.h>
#include <qvbox.h>
#include <qdialog.h>
#include <qlayout.h>


class Shell : public QDialog {
Q_OBJECT
   private:
      QProcess*  m_process;
      QLineEdit* m_txtCommand;
      QTextEdit* m_txtDisplay;
      QVBox*  vbox;
      QString cmd_params;
      QString cmd_command;
   public:

      Shell(const QString& command ,const QString& params ,QWidget *parent = 0, const char *name = 0)
   : QDialog(parent, name)
   {

       vbox = new QVBox(this);
       m_process    = new QProcess(this);
       m_txtDisplay = new QTextEdit(this);

  
       QHBoxLayout *mainLayout = new QHBoxLayout(this);
       mainLayout->addWidget(m_txtDisplay);

        cmd_params = params;
        cmd_command = command;

       m_txtCommand = new QLineEdit("ping 127.0.0.1 -c 3", vbox);




       connect(m_process, SIGNAL(readyReadStdout()),  SLOT(slotDataOnStdout()) );
       connect(m_txtCommand, SIGNAL(returnPressed()), SLOT(slotReturnPressed()) );

//         connect(pcmd, SIGNAL(clicked()), SLOT(slotReturnPressed()));
       slotReturnPressed();
   }

    ~Shell(){
       if (m_process->isRunning()) 
          m_process->tryTerminate();
       hide();
    }

   public slots:
   
      void slotDataOnStdout()
      {
         while (m_process->canReadLineStdout()) {
            m_txtDisplay->append(m_process->readLineStdout() + "\n");
         }
      }

    
      void slotReturnPressed()
      {
         m_process->clearArguments();
            
            QStringList list = QStringList::split(" ",  cmd_command + cmd_params);
           QStringList::iterator it;
           
           for (it = list.begin(); it != list.end(); ++it) {
              m_process->addArgument(*it);
            }
 
          if (!m_process->start()) {
            m_txtDisplay->append("Error!");
          }
      }

      
   protected:
      void closeEvent(QCloseEvent*){
               
         if (!m_process->isRunning()) {
            accept();
         }else{
            m_process->kill();
            accept();
         }
      };
      
      void keyReleaseEvent(QKeyEvent *event){
         if ((event->state() & ControlButton) and (event->key() == Key_C)){
            if (m_process->isRunning()) 
                  m_process->tryTerminate();
               
         }
      }
};

#endif  //_Shell_h_
